package org.sockettest.server;
import org.sockettest.server.UserManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerThread implements Runnable {
    private final UserManager userManager;
    private final Socket socket;
    public ServerThread(Socket socket, UserManager userManager) {
        this.userManager = userManager;
        this.socket = socket;
        userManager.AddUser(socket);
    }


    @Override
    public void run() {
        System.out.println("Thread");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            while (true){
                String str = reader.readLine();
                System.out.println(str);
                if("bye".equals(str)){
                    break;
                }
                userManager.Say(socket, str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            userManager.DelUser(socket);
        }
    }
}
